# -*- coding:utf-8 -*-
from Tkinter import *
from ttk import *

from app_frame import APPControl
from apk_installer import ApkDownloader, ApkSelector, TestToolInstaller
from test_frame import TestControl


class AdbControl:
    def __init__(self, master, adb_manager):
        """构建控制按钮"""
        self.master = Labelframe(master, text="adb 控制")
        # self.master = frame(master)

        self.adb = adb_manager
        self.is_batch = BooleanVar()  # 必须初始化Tk()后才能初始化IntVar()等
        self.is_batch.set(False)

        self.windows_opened = {}

        self.init_ui()

    def init_ui(self):
        frm_apk = Frame(self.master)
        frm_apk.pack(side=TOP, expand=True, padx=4, anchor=W)
        self.btn_local = Button(frm_apk, text="本地安装", command=self.install_local)
        self.btn_local.pack(side=LEFT, padx=4, pady=2)
        self.btn_online = Button(frm_apk, text="下载安装", command=self.install_online)
        self.btn_online.pack(side=LEFT, padx=4, pady=2)
        self.btn_online = Button(frm_apk, text="安装TestTool", command=self.install_testtool)
        self.btn_online.pack(side=LEFT, padx=4, pady=2)
        Button(frm_apk, text="卸载第三方", command=self.uninstall_all).pack(side=LEFT, padx=4, pady=2)

        TestControl(self.action_start, self.master, "测试辅助")

        APPControl(self.action_start, self.master, "PP助手",
                   "com.pp.assistant", "com.pp.assistant.activity.PPMainActivity", "/sdcard/pp/downloader/")

        APPControl(self.action_start, self.master, "豌豆荚",
                   "com.wandoujia.phoenix2", "com.wandoujia.jupiter.activity.HomeActivity", "/sdcard/wandoujia/app/")

        # APPControl(self.action_start, self.master, "淘助",
        #            "com.taobao.appcenter", "com.pp.assistant.PPWelComeActivity", "/sdcard/appcenter/downloader/")

        frm_opt = Frame(self.master)
        frm_opt.pack(side=TOP, expand=False, padx=4, anchor=W)
        Checkbutton(frm_opt, text="批量执行", command=self.set_batch, variable=self.is_batch, onvalue=True, offvalue=False).pack(side=LEFT, padx=4, pady=2)
        self.master.pack(side=TOP, expand=False, padx=4, pady=4, anchor=W, fill="x")

    def set_batch(self):
        self.adb.is_batch = self.is_batch.get()

    def action_start(self, action, **kwargs):
        return self.send_action(action, is_batch=self.is_batch.get(), **kwargs)

    def send_action(self, action, is_batch=False, **kwargs):
        return self.adb.receive_action(action, is_batch, **kwargs)

    def install_local(self):
        try:
            self.windows_opened["apk_selector"].focus()
        except:
            self.windows_opened["apk_selector"] = Toplevel(self.master)
            self.windows_opened["apk_selector"].geometry('560x400')  # 是x 不是*
            self.windows_opened["apk_selector"].title("Install From Local Folder")
            ApkSelector(self.action_start, self.windows_opened["apk_selector"])

    def install_online(self):
        try:
            self.windows_opened["apk_downloader"].focus()
        except:
            self.windows_opened["apk_downloader"] = Toplevel(self.master)
            self.windows_opened["apk_downloader"].geometry('560x400')  # 是x 不是*
            self.windows_opened["apk_downloader"].title("Install From PPCenter")
            ApkDownloader(self.action_start, self.windows_opened["apk_downloader"])

    def uninstall_all(self):
        self.send_action("adb_uninstall_all", False)

    def install_testtool(self):
        installer = TestToolInstaller(self.action_start)
        installer.install()
